package com.vueblog.webapi.utils;

import com.vueblog.webapi.component.LocaleMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;


/**
 * code：信息的键，properties中的key messages.properties，找不到去其他两个当中找直到找到为止
 * 禁止写入同一个key对应不同value
 **/
@Component
public class I18nUtils {
    private static final String LANG_EN = "en";
    private static final String LANG_ZH = "zh";

    @Autowired
    private LocaleMessage localeMessage;

    private static LocaleMessage staticLocalMessage;

    @PostConstruct
    private void init() {
        setLocaleMessage(localeMessage);
    }

    private void setLocaleMessage(LocaleMessage localeMessage) {
        setStaticLocalMessage(localeMessage);
    }

    private static synchronized void setStaticLocalMessage(LocaleMessage localeMessage) {
        I18nUtils.staticLocalMessage = localeMessage;
    }

    public static Locale getCurrentLocale() {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return RequestContextUtils.getLocale(req);
    }

    public static boolean isEn() {
        return getCurrentLocale().getLanguage().equalsIgnoreCase(LANG_EN);
    }

    public static boolean isZh() {
        return getCurrentLocale().getLanguage().equalsIgnoreCase(LANG_ZH);
    }

    public static String getFieldSuffix() {
        if (isEn()) {
            return "_en";
        }
        return "";
    }

    /**
     * 获取key
     *
     * @param key
     * @return
     */
    public static String getKey(String key) {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String msg = getKey(key, RequestContextUtils.getLocale(req));
        if (StringUtils.isEmpty(msg)) {
            return key;
        }
        return msg;
    }


    /**
     * 获取指定哪个配置文件下的key
     *
     * @param key
     * @param local
     * @return
     */
    public static String getKey(String key, Locale local) {
        return staticLocalMessage.getMessage(key, local);
    }

    /**
     * 获取指定哪个配置文件下的key ,格式化返回
     *
     * @param key
     * @param args
     * @param local
     * @return
     */
    public static String getKey(String key, Object[] args, Locale local) {
        return staticLocalMessage.getMessage(key, args, local);
    }

    /**
     * 获取指定哪个配置文件下的key ,格式化返回
     *
     * @param key
     * @param args
     * @return
     */
    public static String getKey(String key, Object[] args) {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String msg = getKey(key, args, RequestContextUtils.getLocale(req));
        if (StringUtils.isEmpty(msg)) {
            return key;
        }
        return msg;
    }
}